<?php

namespace App\Http\Controllers\Wechat;

use App\Http\Controllers\Controller;
use App\Http\Requests\ExperReplyUser;
use App\Models\ExpertConfig;
use App\Models\ExpertCons;
use App\Models\ExpertRecords;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class ExpertController extends Controller
{
    // 接诊记录
    public function index()
    {
        $result = ExpertCons::whereToUserId(auth('wechat')->id())
            ->orderByDesc('id')
            ->paginate($this->limit);
        return $this->writeJson(0, $result->items());
    }

    // 病员列表
    public function patient(Request $request)
    {
        $model = ExpertCons::whereToUserId(auth('wechat')->id())
            ->orderByDesc('id');
        if ($name = $request->post('name')) {
            $model->where('name', 'like', "%{$name}%");
        }
        $result = $model->paginate($this->limit);
        return $this->writeJson(0, $result->items());
    }

    // 回复病员
    public function reply(ExperReplyUser $request)
    {
        $expertCons = ExpertCons::whereToUserId(auth('wechat')->id())
            ->where('id', $request->post('expert_cons_id'))
            ->first();
        if (empty($expertCons)) {
            return $this->writeJson(500, null, '数据不存在');
        }
        if ($expertCons->status == 0) {
            return $this->writeJson(500, null, '已结束，无法再回复');
        }

        try {
            DB::beginTransaction();
            $model = new ExpertRecords();
            $model->to_user_id = auth('wechat')->id();
            $model->expert_cons_id = $request->post('expert_cons_id');
            $model->content = $request->post('content');
            $model->save();

            $expertCons->status = 2;
            $expertCons->save();

            DB::commit();
            return $this->writeJson(0, null, '回复成功');
        } catch (\Exception $exception) {
            DB::rollBack();
            \Log::error($exception);
        } catch (\Throwable $e) {
            DB::rollBack();
            \Log::error($e);
        }
        return $this->writeJson(500, null, '回复失败');
    }

    // 获取接诊配置
    public function config()
    {
        $config = ExpertConfig::whereUserId(auth('wechat')->id())->first();
        return $this->writeJson(0, $config);
    }

    // 修改接诊配置
    public function configEdit(Request $request)
    {
        $config = ExpertConfig::whereUserId(auth('wechat')->id())->first();
        if (empty($config)) {
            $config = new ExpertConfig();
        }

        $config->user_id = auth('wechat')->id();
        $config->day_0 = $request->post('day_0') ? 1 : 0;
        $config->day_1 = $request->post('day_1') ? 1 : 0;
        $config->day_2 = $request->post('day_2') ? 1 : 0;
        $config->day_3 = $request->post('day_3') ? 1 : 0;
        $config->day_4 = $request->post('day_4') ? 1 : 0;
        $config->day_5 = $request->post('day_5') ? 1 : 0;
        $config->day_6 = $request->post('day_6') ? 1 : 0;
        $config->online_am = $request->post('online_am') ? 1 : 0;
        $config->online_pm = $request->post('online_pm') ? 1 : 0;
        $config->online_night = $request->post('online_night') ? 1 : 0;
        $config->num = intval($request->post('num'));
        $config->save();
        return $this->writeJson(0, null, '设置成功');
    }
}
